#!/usr/bin/python3
# Converts 128x64x1bpp bitmaps into C header data
# --
# Chris Baird <cjb@brushtail.apana.org.au> did this, 2017-2024.
# Licenced according to the GPLv3

import sys
import binascii

mem = [0] * 1024
pixels = ( 1, 2, 4, 8, 16, 32, 64, 128 )

def memplot (x, y):
  plot_addr = ((y >> 3)*128) + x
  mem[plot_addr] |= pixels[y & 7];


from PIL import Image, ImageFilter
im = Image.open(sys.argv[1]).convert('RGB')
if im.size != (128,64):
  print ("image not 128x64-- get the Gimp out, matey. exiting")
  sys.exit()

for y in range(0,64):
  for x in range(0,128):
    if not im.getpixel((x,y))[0]:
      memplot(x,y)


p = 0
l = 0
lb = -1
out = ""

for p in range(0,1024):
  b = mem[p]
  out = out + chr(b)

print ("\n#define UNCOMPRESSED_SPLASH 1")
print ("const unsigned char logo_bitmap[] = { ")

tb = 0
for i in range(0,len(out)):
  if tb == 0:
    line = "    "
  c = str(ord(out[i:i+1]))
  line = line + str(c)+ ","
  tb = tb + 1
  if (i == len(out)-1) | len (line) > 70:
    print (line)
    tb = 0
    # TODO - add commas properly

print (line.rstrip(","), "\n};\n")

######################################################################
